package com.example.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类，需要使用configuration注解
 */
@Configuration
public class RabbitConfig {
    //定义三个交换机

    /**
     * 直连交换机
     *
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("DIRECT_EXCHANGE");
    }

    /**
     * 主题交换机
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("TOPIC_EXCHANGE");
    }

    /**
     * 广播交换机
     *
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("FANOUT_EXCHANGE");
    }

    //定义四个队列
    @Bean
    public Queue firsQueue() {
        return new Queue("FIST_QUEUE");
    }

    @Bean
    public Queue secondQueue() {
        return new Queue("SECOND_QUEUE");
    }

    @Bean
    public Queue thirdQueue() {
        return new Queue("THIRD_QUEUE");
    }

    @Bean
    public Queue fourthQueue() {
        return new Queue("FOURTH_QUEUE");
    }

    //定义四个绑定关系
    @Bean
    public Binding bindFirst(@Qualifier("firsQueue") Queue queue,
                             @Qualifier("directExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("zbb.test");
    }

    @Bean
    public Binding bindSecond(@Qualifier("secondQueue") Queue queue,
                              @Qualifier("topicExchange") TopicExchange topicExchange) {
        return BindingBuilder.bind(queue).to(topicExchange).with("*.zbb.*");
    }

    @Bean
    public Binding bindThird(@Qualifier("thirdQueue") Queue queue,
                             @Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }

    @Bean
    public Binding bindFourth(@Qualifier("fourthQueue") Queue queue,
                              @Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }

}